﻿

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using RWG.Silverlight.Components.SPDiscussion;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Browser;
namespace RWG.Silverlight.SPDiscussion
{
    public partial class MainPage : UserControl
    {
        private ViewModels.MainPageVM mainPageVM;
        public ViewModels.MainPageVM MainPageVM
        {
            get { return mainPageVM; }
            set { mainPageVM = value; }
        }
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
           
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            MainPageVM = new ViewModels.MainPageVM();
            MainPageVM.ListID = App.ListID;
            this.DataContext = MainPageVM;
            DiscusssionManager discussions = new DiscusssionManager();
            discussions.GetDiscussionHeaders(App.ListID, mainPageVM.Discussions, null,this.Dispatcher);
        }

     
        private void dgDiscussions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DiscusssionManager.DiscussionHeader discussion = dgDiscussions.SelectedItem as DiscusssionManager.DiscussionHeader;
            HtmlElement messageDisplayDiv = HtmlPage.Document.GetElementById(App.MessageControlName);
            messageDisplayDiv.SetProperty("innerHTML",MainPageVM.GetMessageBody(discussion));
            mainPageVM.Messages.Clear();
            DiscusssionManager d = new DiscusssionManager();
            //d.GetDiscussionMessages(App.ListID,discussion,null,this.Dispatcher);
            // d.GetDiscussionMessages(App.ListID,MainPageVM.Messages, discussion.FileRef,null,this.Dispatcher,discussion.Thread);
            discussion.Messages.Clear();
            d.GetDiscussionMessages(App.ListID, discussion, null, this.Dispatcher);
        }

        private void myTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            HtmlElement messageDisplayDiv = HtmlPage.Document.GetElementById(App.MessageControlName);
            messageDisplayDiv.SetProperty("innerHTML", MainPageVM.GetMessageBody(e.NewValue as DiscusssionManager.DiscussionMessage));
        }
    }
}
